package com.example.liqm.eventbus;

import android.app.usage.UsageEvents;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv_frist);
        button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
        //注册事件
        EventBus.getDefault().register(this);

    }
    //接收方法 这个是在主线程发送  主线程接收的
    //参数为object时  每次发送都会走这个方法，这样可以减少同一个页面多写接收的方法
  /*  @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(Object messageEvent){
        if(messageEvent instanceof MessageEvent){
            MessageEvent event = (MessageEvent) messageEvent;
            textView.setText(event.getMessage());
        }else if(messageEvent instanceof MessageEvent2){
            MessageEvent2 event2 = (MessageEvent2) messageEvent;
            textView.setText(event2.getMessage());
        }
    }*/
//通过参数来确定调用那个接收方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){

            textView.setText(messageEvent.getMessage());
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent2 messageEvent2){

        textView.setText(messageEvent2.getMessage());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消事件
        EventBus.getDefault().unregister(this);
    }
}
